﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;
using SlimCode.Windows.Controls.Mobile;

namespace SlimCode.SlimPasswords
{
  internal partial class EditPasswordItemScreen : UserControl, IScreen
  {
    private IScreenHost m_host;
    private DateTime m_lastModified = DateTime.MaxValue;  // Do not report "changed" if not set

    public EditPasswordItemScreen()
    {
      InitializeComponent();
    }

    public EditPasswordItemScreen( PasswordItem item, bool isNew )
      : this()
    {
      this.Item = item;
      this.IsNew = isNew;
    }

    public bool Changed
    {
      get { return ( m_lastModified < this.Item.LastModified ); }
    }

    public bool IsNew { get; private set; }

    public PasswordItem Item { get; private set; }

    private void doneMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.OK );
      }
    }

    private void cancelMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.Cancel );
      }
    }

    private void passwordTextBox_GotFocus( object sender, EventArgs e )
    {
      passwordTextBox.PasswordChar = '\0';
      this.AnyControl_GotFocus( sender, e );
    }

    private void passwordTextBox_LostFocus( object sender, EventArgs e )
    {
      passwordTextBox.PasswordChar = '*';
    }

    private void FillInformation()
    {
      categoryComboBox.Items.Clear();
      userNameComboBox.Items.Clear();
      emailComboBox.Items.Clear();

      this.UpdateComboBoxes();

      if( this.Item != null )
      {
        m_lastModified = this.Item.LastModified;

        titleTextBox.Text = this.Item.Title;
        categoryComboBox.Text = this.Item.Category;
        userNameComboBox.Text = this.Item.UserName;
        emailComboBox.Text = this.Item.Email;
        passwordTextBox.Text = this.Item.Password;
        activeCheckBox.Checked = this.Item.Active;

        urlLabel.Text = this.Item.UrlAddress;
        notesLabel.Text = this.Item.Notes;
      }
    }

    private void UpdateComboBoxes()
    {
      SortedList<string, string> categories = new SortedList<string, string>( StringComparer.InvariantCultureIgnoreCase );
      SortedList<string, string> userNames = new SortedList<string, string>( StringComparer.InvariantCultureIgnoreCase );
      SortedList<string, string> emails = new SortedList<string, string>( StringComparer.InvariantCultureIgnoreCase );

      foreach( PasswordItem item in Program.ActiveStore.Passwords )
      {
        if( !string.IsNullOrEmpty( item.Category ) )
        {
          if( !categories.ContainsKey( item.Category ) )
          {
            categories.Add( item.Category, item.Category );
          }
        }

        if( !string.IsNullOrEmpty( item.UserName ) )
        {
          if( !userNames.ContainsKey( item.UserName ) )
          {
            userNames.Add( item.UserName, item.UserName );
          }
        }

        if( !string.IsNullOrEmpty( item.Email ) )
        {
          if( !emails.ContainsKey( item.Email ) )
          {
            emails.Add( item.Email, item.Email );
          }
        }
      }

      categoryComboBox.DataSource = categories.Values.ToArray<string>();
      userNameComboBox.DataSource = userNames.Values.ToArray<string>();
      emailComboBox.DataSource = emails.Values.ToArray<string>();
    }

    private void AnyControl_GotFocus( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        Program.ToggleInputPanelIf( m_host.MainForm.MainInputPanel, ( sender is TextBox ) || ( sender is ComboBox ) );
      }
    }

    private void generatePasswordButton_Click( object sender, EventArgs e )
    {
      string password = Program.Settings.PasswordScheme.GeneratePassword();

      if( MessageBox.Show(
        string.Format( "The generated password is: {0}\nDo you want to keep this password?", password ),
        "Confirm",
        MessageBoxButtons.YesNo,
        MessageBoxIcon.Question,
        MessageBoxDefaultButton.Button1 ) == DialogResult.Yes )
      {
        passwordTextBox.Text = password;
        passwordTextBox.Focus();
      }
    }

    private void moreButton_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.ShowScreen( new EditNotesScreen( urlLabel.Text, notesLabel.Text ) );
      }
    }

    #region IScreen Members

    public Control Control
    {
      get { return this; }
    }

    public Control DefaultFocus
    {
      get { return titleTextBox; }
    }

    public MainMenu Menu
    {
      get { return mainMenu; }
    }

    public string Title
    {
      get { return "slimPASSWORDS - Edit"; }
    }

    public bool Hiding( DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        if( this.Item != null )
        {
          if( this.Item.Title != titleTextBox.Text )
          {
            this.Item.Title = titleTextBox.Text;
          }

          if( this.Item.Category != categoryComboBox.Text )
          {
            this.Item.Category = categoryComboBox.Text;
          }

          if( this.Item.UserName != userNameComboBox.Text )
          {
            this.Item.UserName = userNameComboBox.Text;
          }

          if( this.Item.Email != emailComboBox.Text )
          {
            this.Item.Email = emailComboBox.Text;
          }

          if( this.Item.Password != passwordTextBox.Text )
          {
            this.Item.PreviousPassword = this.Item.Password;
            this.Item.Password = passwordTextBox.Text;
          }

          this.Item.Active = activeCheckBox.Checked;

          if( this.Item.UrlAddress != urlLabel.Text )
          {
            this.Item.UrlAddress = urlLabel.Text;
          }

          if( this.Item.Notes != notesLabel.Text )
          {
            this.Item.Notes = notesLabel.Text;
          }
        }
      }

      return true;
    }

    public void HidingChild( IScreen child, DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        EditNotesScreen editNotesScreen = child as EditNotesScreen;

        if( editNotesScreen != null )
        {
          urlLabel.Text = editNotesScreen.Url;
          notesLabel.Text = editNotesScreen.Notes;
        }
      }
    }

    public void Showing( IScreenHost host )
    {
      m_host = host;

      this.FillInformation();
    }

    #endregion
  }
}
